﻿# coding: utf-8

import ConfigParser
import os
import string

import wx
import FileSystem

class GlobalSetting:
    def __init__(self):
        # Working directory
        self._workDir = os.getcwd()

        # Model assets directory
        self._modelDir = ''

        # Effect assets directory
        self._effectDir = ''
        
        # Texture directory
        self._textureDir = ''

        # Editor UI layout
        self._editorUILayout = ''

        # Visible panel list
        self._visiblePanelList = set([])

        # Shader file used by terrain
        self._terrainShaderFile = ''

        # Default terrain layer texture file
        self._defaultTerrainLayer = ''
        
        # Height brush list.
        self._terrainHeightBrushList = []
        
        # Layer brush list.
        self._terrainLayerBrushList = []
        
        # Terrain layer list.
        self._terrainLayerList = []
        
        # Water shader file.
        self._waterShaderFile = ''
        
        # Water bump texture file
        self._waterBumpTextureFile = u''

        # Lanauage
        self._language = u'Chinese'

    def Save(self):
        try:
            conf = ConfigParser.ConfigParser()

            conf.add_section('Setting')
            conf.set('Setting', 'WorkDir', self._workDir)
            conf.set('Setting', 'ModelDir', self._modelDir)
            conf.set('Setting', 'EffectDir', self._effectDir)
            conf.set('Setting', 'TextureDir', self._textureDir)
            conf.set('Setting', 'EditorUILayout', self._editorUILayout)
            conf.set('Setting', 'VisiblePanelList', string.join(self._visiblePanelList, ';').encode('utf-8'))
            conf.set('Setting', 'TerrainShaderFile', self._terrainShaderFile)
            conf.set('Setting', 'DefaultTerrainLayer', self._defaultTerrainLayer)
            conf.set('Setting', 'TerrainHeightBrushList', string.join(self._terrainHeightBrushList, ';').encode('utf-8'))
            conf.set('Setting', 'TerrainLayerBrushList', string.join(self._terrainLayerBrushList, ';').encode('utf-8'))
            conf.set('Setting', 'TerrainLayerList', string.join(self._terrainLayerList, ';').encode('utf-8'))
            conf.set('Setting', 'WaterShaderFile', self._waterShaderFile)
            conf.set('Setting', 'WaterBumpTextureFile', self._waterBumpTextureFile)
            conf.set('Setting', 'Language', self._language)

            conf.write(open('config.ini', 'w'))

            return True

        except:
            SHOW_TRACE()
            return False

    def Load(self):
        try:
            conf = ConfigParser.ConfigParser()
            conf.read('config.ini')

            self._workDir = conf.get('Setting', 'WorkDir').strip()
            self._modelDir = conf.get('Setting', 'ModelDir').strip()
            self._effectDir = conf.get('Setting', 'EffectDir').strip()
            self._textureDir = conf.get('Setting', 'TextureDir').strip()
            self._editorUILayout = conf.get('Setting', 'EditorUILayout').strip()
            self._visiblePanelList = conf.get('Setting', 'VisiblePanelList').decode('utf-8').strip().split(';')
            self._terrainShaderFile = conf.get('Setting', 'TerrainShaderFile').strip()
            self._defaultTerrainLayer = conf.get('Setting', 'DefaultTerrainLayer').strip()
            self._terrainHeightBrushList = conf.get('Setting', 'TerrainHeightBrushList').decode('utf-8').strip().split(';')
            self._terrainLayerBrushList = conf.get('Setting', 'TerrainLayerBrushList').decode('utf-8').strip().split(';')
            self._terrainLayerList = conf.get('Setting', 'TerrainLayerList').decode('utf-8').strip().split(';')
            self._waterShaderFile = conf.get('Setting', 'WaterShaderFile').strip()
            self._waterBumpTextureFile = conf.get('Setting', 'WaterBumpTextureFile').strip()
            self._language = conf.get('Setting', 'Language').strip()

            try:
                self._visiblePanelList.remove('')
            except:
                pass
                
            try:
                self._terrainHeightBrushList.remove('')
            except:
                pass

            return True

        except:
            SHOW_TRACE()
            return False

    def Apply(self):
        try:
            FileSystem.SetRoot(self._workDir)
            PANEL_ASSETS.OnUpdateTreeList()

            return True
        except:
            return False

__builtins__['GLOBAL_SETTING'] = GlobalSetting()

def __RemoveRootPath(path):
    if not path.startswith(GLOBAL_SETTING._workDir):
        raise 'ERROR: path is invalid.'
        
    return path[len(GLOBAL_SETTING._workDir):]

__builtins__['REMOVE_ROOT_PATH'] = __RemoveRootPath